package com.teradata.olingo.controller;

import com.teradata.olingo.listener.ODataAsynListener;
import com.teradata.olingo.processor.AsyncRequestProcessor;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Project: cmcchq
 * @Description:
 * @Version 1.0
 * @Throws :
 * @Author: <li>2019/5/7 lyndon Create 1.0
 * @Copyright (c)2019-2019 版权所有
 * @Modified By:
 */
@WebServlet(name = "chinabond", urlPatterns = "/chinabond.svc/*", asyncSupported = true)
public class EdmAsynServlet extends HttpServlet {

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        System.out.println("Servlet thread name:" + Thread.currentThread().getName());
        req.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
        AsyncContext asyncContext = req.startAsync();
        asyncContext.addListener(new ODataAsynListener());
        asyncContext.setTimeout(5 * 1000L);

        // max 10 seconds
        int secs = 10000;

        ThreadPoolExecutor executor = (ThreadPoolExecutor) req
                .getServletContext().getAttribute("executor");

        executor.execute(new AsyncRequestProcessor(asyncContext, secs));
    }
}
